<template>
  <div>
    <VRow class="lf-config-row">
      <VCol cols="12">
        <VTextField
          v-model="chainId"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="链路ID"
          label="链路ID"
        />
      </VCol>
      <VCol cols="12">
        <VTextField
          v-model="chainName"
          class="lf-config-item"
          counter
          maxlength="50"
          color="success"
          hint="链路名称"
          label="链路名称"
        />
      </VCol>
      <VCol cols="12">
        <VAutocomplete
          v-model="ivyElId"
          class="flow-mt-12"
          variant="outlined"
          label="选择EL表达式"
          :items="elOptions"
          item-title="elName"
          item-value="id"
          clearable
        />
      </VCol>
    </VRow>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const props = defineProps({		
  currRow: Object,
  addUrl: String,
  updateUrl: String,
})

const emits = defineEmits(['refreshPage'])

const addUrl = ref(props.addUrl)
const updateUrl = ref(props.updateUrl)

const currRow = ref(props.currRow)
const row = currRow.value

const isEdit = ref(!!row.id)

const id = ref(row.id || '')
const chainId = ref(row.chainId || '')
const chainName = ref(row.chainName || '')
const ivyElId = ref(row.ivyElId || '')

const getUrl = () => {
  if(isEdit.value){
    return updateUrl.value
  }
  
  return addUrl.value
}

const saveItem = async () => {
  const res = await $ivyApi.post(getUrl(),{
    id: id.value,
    chainId: chainId.value,
    chainName: chainName.value,
    ivyElId: ivyElId.value,
  })
  emits('refreshPage', res)
}

defineExpose({
  saveItem,
})

const elOptions = reactive([])

onMounted(() => {
  optionElQuery()
})

const optionElQuery = async () => {
  const res = await $ivyApi.post(API_URLS.lfElOptionEl,{})
  elOptions.push(...res.data)
}
</script>

<style scoped>
.lf-config-row .v-col {
  padding-block-end: 3px;
}

.lf-config-item {
  padding-block-start: 8px;
}
</style>
